<?php

define( 'DS' , DIRECTORY_SEPARATOR );

define( 'CROOT' , dirname( __FILE__ ) . DS );
defined('AROOT') || define( 'AROOT' , dirname( CROOT ) . DS );

set_include_path(get_include_path() . PATH_SEPARATOR . CROOT);

defined('DEBUG_PAGES') or define('DEBUG_PAGES', '5');

error_reporting(E_ALL^E_NOTICE);
ini_set( 'display_errors' , true );

include_once( CROOT . DS . 'core.config.php' );
include_once( CROOT . DS . 'core.function.php' );
include_once( CROOT . DS . 'common.function.php' );
include_once( CROOT . DS . 'sys.php' );

defined('USE_OAUTH') and include_once( CROOT . DS . 'oauth' . DS . 'base.class.php' );


@include_once( p(AROOT, 'config', 'app.config.php') );
@include_once( p(AROOT, 'lib', 'app.function.php') );

// 定义当前请求的系统常量
define('NOW_TIME',      $_SERVER['REQUEST_TIME']);
define('REQUEST_METHOD',$_SERVER['REQUEST_METHOD']);
define('IS_GET',        REQUEST_METHOD =='GET' ? true : false);
define('IS_POST',       REQUEST_METHOD =='POST' ? true : false);
define('IS_PUT',        REQUEST_METHOD =='PUT' ? true : false);
define('IS_DELETE',     REQUEST_METHOD =='DELETE' ? true : false);
define('IS_AJAX',       ((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') || !empty($_POST[C('VAR_AJAX_SUBMIT')]) || !empty($_GET[C('VAR_AJAX_SUBMIT')])) ? true : false);

defined('REDIRECT_404') or define('REDIRECT_404', '/');

// HTTP 头，会话等启动。
if(strpos('gzip',$_SERVER['HTTP_ACCEPT_ENCODING']) !== FALSE)  ob_start("ob_gzhandler");
if(defined('SESSION_DOMAIN')){
	$sp = session_get_cookie_params();
	session_set_cookie_params($sp['lifetime'], $sp['path'], SESSION_DOMAIN, $sp['secure'], $sp['httponly']);
}
defined('SESSION_NAME') && session_name(SESSION_NAME); 
session_start();

{ // 网站调试处理
	if (v('debug') == date('mdY'))  session('indebug', true); 
	if (session('indebug')) define('DEBUG', true);
	include_once( CROOT . DS . 'debug.php' );	
}

header('P3P:CP="NOI ADM DEV PSAi COM NAV OUR OTR STP IND DEM"');

// 退出处理
register_shutdown_function('appExitInstance');
// 错误处理
set_error_handler('Debug::Error');
set_exception_handler('Debug::Exception');

// 预先处理
$file = p(AROOT, "action", 'InitInstance.php');
if (file_exists($file)) require($file); 

assign('target', 'rest');
if( is_ajax_request() ) assign('target', 'ajax'); 
elseif( is_mobile_request() ) assign('target', 'mobile'); 
else assign('target', 'web'); 

//完整请求串 /dasd/admin?a=c&d=23...
$uri = $_SERVER['REQUEST_URI'];
//指令 /dasd/admin
//参数字符串 ?a=c&d=234
$i = strpos($uri, '?');
if($i){
	$actions = strtolower(substr($uri,0,$i));
	$params  = substr($uri,$i + 1);
}
else {
	$actions = $uri;
	$params = '';
}
$actions = explode('/', $actions);
foreach ($actions as $k => $v) {
	if (empty($v)) unset ($actions[$k]);
	else $actions[$k] = preg_replace($config['url_ext'], '', $v);
}

$params = explode('&', $params);
foreach($params as $p){
	$k = $p; $v = true;
	if( strpos($p, '=') ) list($k,$v) =  explode('=', $p) ;
	param( $k, $v );
}

// 网站暂停处理
$file = AROOT . DS . 'msg';
if (file_exists($file)) {
	$pa = param('admin');	$ca = cookie('admin');
	$pa || $ca || exit(require($file));
	$pa && cookie('admin',123456);
}


//二级域名
$subDomain  = strtolower(substr($_SERVER['HTTP_HOST'],0,strpos($_SERVER['HTTP_HOST'],'.')));


// 通关数据
template( empty($actions) ? $config['default_action'] : implode('_', $actions));// 模板文件

$actions[] = $config['default_action'];

// layout('default');		   // 布局类型
// assign('view', implode('_', $actions));// 模板文件
// assign('layout', 'default');		   // 布局类型

// 预先处理
$file = p(AROOT, "action", 'beforeaction.php');
if (file_exists($file)) require($file); 

// 循环处理 action.
$actfile = '';
define('SEP', '_');
while($actions){
	$action = @array_shift($actions);
	$actfile .= (($actfile) ? SEP : '') . $action;
	$file = p(AROOT, "action", $actfile . '.action.php');
	if (file_exists($file)) require($file); 
	else{
		$file = p(CROOT, "action", $actfile . '.action.php');
		if (file_exists($file)) require($file); 
		else { if (!empty($actions)) { _404(); break; } };
	}
}

// 后置处理
$file = p(AROOT, "action", 'afteraction.php');
if (file_exists($file)) require($file); 


// 输出页面
display(); 
